//
//  WebViewController.swift
//  totoCloud
//
//  Created by 黄龙 on 2023/1/9.
//

import UIKit
import WebKit

class WebViewController: UIViewController {
    var wkWebView: WKWebView = {
        let config = WKWebViewConfiguration.init()
        let preference = WKPreferences()// 创建设置对象
        //最小字体大小 当将javaScriptEnabled属性设置为NO时，可以看到明显的效果
        preference.minimumFontSize = 0
        //设置是否支持javaScript 默认是支持的
        preference.javaScriptEnabled = true
        // 在iOS上默认为NO，表示是否允许不经过用户交互由javaScript自动打开窗口
        preference.javaScriptCanOpenWindowsAutomatically = true
        //如果要加载本地html时，必须加上这一句，否则有可能页面显示不正常
        preference.setValue(true, forKey: "allowFileAccessFromFileURLs")
        config.preferences = preference

        // 是使用h5的视频播放器在线播放(YES), 还是使用原生播放器全屏播放(NO)
        config.allowsInlineMediaPlayback = true
        //设置视频是否需要用户手动播放  设置为NO则会允许自动播放
//        config.requiresUserActionForMediaPlayback = true
        //设置是否允许画中画技术 在特定设备上有效
        config.allowsPictureInPictureMediaPlayback = true
        //设置请求的User-Agent信息中应用程序名称 iOS9后可用
        config.applicationNameForUserAgent = Bundle.main.bundleIdentifier
        
        let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: screen_W, height: screen_H), configuration: config)
        // UI代理
//        webView.UIDelegate = self;
        // 导航代理
//        webView.navigationDelegate = self;
        // 是否允许手势左滑返回上一级, 类似导航控制的左滑返回
        webView.allowsBackForwardNavigationGestures = true
        //可返回的页面列表, 存储已打开过的网页
        return webView
    }()
    
    lazy var labelTitle:UILabel = {
        let label = UILabel()
        label.font = .boldSystemFont(ofSize: 16)
        label.textColor = .label
        label.textAlignment = .center
        return label
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .systemGroupedBackground
        // Do any additional setup after loading the view.
        initUI()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        var statusBarH = 15.0
        if self.modalPresentationStyle == .fullScreen{
            statusBarH = UIDevice.ui_safeDistanceTop()
        }
        self.wkWebView.frame = CGRect(x: 0 , y: statusBarH + 45 , width: screen_W, height: screen_H - statusBarH - 45 - UIDevice.ui_safeDistanceBottom())
    }
    
    deinit {
        print("ViewController----deinit")
        self.wkWebView.load(URLRequest(url: URL(string: "about:blank")!))
    }

    
    func initUI(){
        self.view.addSubview(self.wkWebView)
        
        var statusBarH = 15.0
        if self.modalPresentationStyle == .fullScreen{
            statusBarH = UIDevice.ui_safeDistanceTop()
        }
        let cancelButton = UIButton(frame: CGRect(x: screen_W-42, y: statusBarH, width: 42, height: 42))
        self.view.addSubview(cancelButton)
        cancelButton.setImage(UIImage(named: "toolbar_close_1@2x.png"), for: .normal)
        cancelButton.addActionWithBlock { sender in
            self.wkWebView.load(URLRequest(url: URL(string: "about:blank")!))
            self.dismiss(animated: true)
        }
    }
    
    func loadUrlStr(urlstr: String,title: String? = nil){
        let url = URL(string: urlstr)
        let request = URLRequest(url: url!)
        self.wkWebView.load(request)
        if title != nil {
            self.view.addSubview(self.labelTitle)
            self.labelTitle.text = title
            var statusBarH = 15.0
            if self.modalPresentationStyle == .fullScreen{
                statusBarH = UIDevice.ui_safeDistanceTop()
            }
            labelTitle.frame = CGRect(x: 42, y: statusBarH, width: screen_W - 2*42, height: 42)
        }
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
